package com.ficus.road.maintain.core.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DelayFrameDTO implements Delayed {
    private long delayTime;
    private FrameDTO frameDTO;

    public static DelayFrameDTO fromFrameDTO(FrameDTO frameDTO, long delayTime) {
        DelayFrameDTO delayFrameDTO = new DelayFrameDTO();
        delayFrameDTO.setDelayTime(delayTime);
        delayFrameDTO.setFrameDTO(frameDTO);
        return delayFrameDTO;
    }

    public FrameDTO getFrameDTO() {
        return frameDTO;
    }

    public void setFrameDTO(FrameDTO frameDTO) {
        this.frameDTO = frameDTO;
    }

    public long getDelayTime() {
        return delayTime;
    }

    public void setDelayTime(long delayTime) {
        this.delayTime = delayTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long diffTime = this.getFrameDTO().getSendTimeStamp() + delayTime * 1000 - System.currentTimeMillis();
        return unit.convert(diffTime, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        DelayFrameDTO delayFrameDTO = (DelayFrameDTO) o;
        if (this.getFrameDTO().getSendTimeStamp() < delayFrameDTO.getFrameDTO().getSendTimeStamp()) {
            return -1;
        } else if (this.getFrameDTO().getSendTimeStamp().equals(delayFrameDTO.getFrameDTO().getSendTimeStamp())) {
            return 0;
        } else {
            return 1;
        }
    }
}
